﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Helpers;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.Results;
using System.Web.Mvc;
using David.Framework.Core.Enums;
using David.Framework.Core.Helper;
using David.Framework.Web.Security;
using David.WebSite.Data;
using ActionFilterAttribute = System.Web.Http.Filters.ActionFilterAttribute;

namespace David.Framework.Web.Filter
{

    public class LoggedApi : ActionFilterAttribute
    {

        public LoggedApi()
        {
        }

        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var url = actionContext.RequestContext.Url;
            if (!DavidFormsAuthentication.IsLogin())
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK);
                actionContext.Response.Content = new StringContent(Json.Encode(new Response
                {
                    code = (int)ResponseEnum.LogInError,
                    msg = ResponseEnum.LogInError.ToString()
                }), Encoding.UTF8, "application/json");
            }
            else
            {
                //验证用户状态
                var userid = DavidFormsAuthentication.GetUserId();
                var userinfo = AdminsDataService.GetUseInfoByUserId(userid);
                if (!userinfo.IsEnable)
                {
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK);
                    actionContext.Response.Content = new StringContent(Json.Encode(new Response
                    {
                        code = (int) ResponseEnum.UserClosed,
                        msg = ResponseEnum.UserClosed.ToString(),
                        sub_code = ResponseEnum.UserClosed.ToString(),
                    }), Encoding.UTF8, "application/json");
                }
            }

            base.OnActionExecuting(actionContext);
        }
    }


    /// <summary>
    /// 返回结果
    /// </summary>
    public class Response
    {
        /// <summary>
        /// 返回码
        /// </summary>
        public int code { get; set; }
        /// <summary>
        /// 返回码描述
        /// </summary>
        public string msg { get; set; }
        /// <summary>
        /// 明细返回码
        /// </summary>
        public string sub_code { get; set; }
        /// <summary>
        /// 明细返回码描述
        /// </summary>
        public string sub_msg { get; set; }
    }
}

